namespace ObjectPersistence {
    using System;
    using System.Text;
    using System.Collections;
    using System.Data.Common;
    using System.Transactions;
    using System.Collections.Generic;
    
    public partial class ModuleIntallAssembler : AssemblerBaseUpdatable<KYO.DAL.ModuleIntall, ModuleIntallCriteria, ModuleIntallRowMap, ModuleIntallDomainObjectFactory> {
        public ModuleIntallAssembler() {
            base.connection = new System.Data.SqlClient.SqlConnection(GlobalUtil.Constants.ConnectionString);
        }
        protected override System.Data.Common.DbConnection InstanceConnection {
            get {
                return this.connection;
            }
        }
        /// <summary>Reads a single object from the database corresponding to the object keys which are passed in</summary>
        public KYO.DAL.ModuleIntall ReadSingleByKey(int ID) {
            ModuleIntallCriteria criteria = new ModuleIntallCriteria();
            criteria.Set_ID(ModuleIntallCriteria.AVAILABLE_OPERATORS.EQUALS, ID);
            return this.ReadSingle(criteria);
        }

        internal override void MakePersistent(KYO.DAL.ModuleIntall PersistentObject, bool cascade, System.Data.Common.DbCommand comm) {
            if ((PersistentObject.IsNew == false)) {
                if ((GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Contains(PersistentObject.ROKeyString) 
                            && (PersistentObject.ChangesMade == false))) {
                    return;
                }
                if ((GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Contains(PersistentObject.ROKeyString) == false)) {
                    GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Add(PersistentObject.ROKeyString);
                }
            }
            if (cascade) {
                ModuleAssembler tempModuleAssembler = new ModuleAssembler();

                if ((PersistentObject.IsModuleObjectNull() == false)) {
                    comm.Parameters.Clear();
                    tempModuleAssembler.MakePersistent(PersistentObject.ModuleObject, cascade, comm);
                    if (((PersistentObject.ModuleId == PersistentObject.ModuleObject.ModuleId) 
                                == false)) {
                        PersistentObject.ModuleId = PersistentObject.ModuleObject.ModuleId;
                    }
                }
                tempModuleAssembler.Dispose();
            }
            if ((PersistentObject.IsNew || PersistentObject.ChangesMade)) {
                ModuleIntallUpdateFactory updateFactory = new ModuleIntallUpdateFactory();
                comm.Parameters.Clear();
                if ((PersistentObject.IsNew == false)) {
                    updateFactory.updateCommandText(PersistentObject, comm);
                    int numRows = comm.ExecuteNonQuery();
                    if ((numRows == 0)) {
                        throw new NoRowsUpdated();
                    }
                }
                else {
                    updateFactory.insertCommandText(PersistentObject, comm);
                    comm.CommandText = (comm.CommandText + "; SELECT SCOPE_IDENTITY();");
                    decimal NewIdentityKey = ((decimal)(comm.ExecuteScalar()));
                    PersistentObject.ID = ((int)(NewIdentityKey));
                    GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Add(PersistentObject.ROKeyString);
                    PersistentObject.IsNew = false;
                }
            }
            PersistentObject.ChangesMade = false;
            if (cascade) {
                TabAssembler tempTabAssembler = new TabAssembler();

                if ((PersistentObject.IsTabCollNull() == false)) {
                    for (int i1 = 0; (i1 < PersistentObject.TabColl.Count); i1 = (i1 + 1)) {
                        comm.Parameters.Clear();
                        tempTabAssembler.MakePersistent(PersistentObject.TabColl[i1], cascade, comm);
                    }
                    PersistentObject.TabColl.Clean();
                    this.InsertAddedTabCollMappingTable(PersistentObject, comm);
                    this.RemoveDeletedTabCollMappingTableEntries(PersistentObject, comm);
                    PersistentObject.TabColl.Synch();
                }
                tempTabAssembler.Dispose();
            }
        }

        internal override void Delete(KYO.DAL.ModuleIntall PersistentObject, System.Data.Common.DbCommand comm) {
            this.DeleteTabCollMappingTable(PersistentObject, comm);

            comm.Parameters.Clear();

            ModuleIntallSelectionFactory tempModuleIntallSelectionFactory = new ModuleIntallSelectionFactory();
            string sql = "DELETE FROM [dbo].[ModuleIntall]";
            comm.CommandText = sql;
            tempModuleIntallSelectionFactory.getDefaultSelection(PersistentObject, comm);
            comm.ExecuteNonQuery();
        }

        #region Manage Mapping Tables
        private void InsertAddedTabCollMappingTable(KYO.DAL.ModuleIntall PersistentObject, System.Data.Common.DbCommand comm) {
            comm.CommandText = "IF NOT EXISTS (SELECT 1 FROM [dbo].[ModuleIntallForTab] (nolock) WHERE [ModuleInt" +
                "allId] = @larg0 AND [TabId] = @rarg1) INSERT INTO [dbo].[ModuleIntallForTab] ([M" +
                "oduleIntallId] , [TabId] ) VALUES (@larg0, @rarg1)";
            for (int i = 0; (i < PersistentObject.TabColl.Added.Count); i = (i + 1)) {
                comm.Parameters.Clear();
                ObjectPersistenceHelper.AddParameterWithValue(comm, "@larg0", PersistentObject.ID);
                ObjectPersistenceHelper.AddParameterWithValue(comm, "@rarg1", PersistentObject.TabColl.Added[i].TabId);
                comm.ExecuteNonQuery();
            }
        }

        private void DeleteTabCollMappingTable(KYO.DAL.ModuleIntall PersistentObject, System.Data.Common.DbCommand comm) {
            comm.Parameters.Clear();
            comm.CommandText = "DELETE FROM [dbo].[ModuleIntallForTab] WHERE [ModuleIntallId]=@arg0";
            ObjectPersistenceHelper.AddParameterWithValue(comm, "@arg0", PersistentObject.ID);
            comm.ExecuteNonQuery();
        }

        private void RemoveDeletedTabCollMappingTableEntries(KYO.DAL.ModuleIntall PersistentObject, System.Data.Common.DbCommand comm) {
            comm.CommandText = "DELETE FROM [dbo].[ModuleIntallForTab] WHERE  [ModuleIntallId]=@arg0 AND  [TabId]" +
                "=@rarg1";
            for (int i = 0; (i < PersistentObject.TabColl.Deleted.Count); i = (i + 1)) {
                comm.Parameters.Clear();
                ObjectPersistenceHelper.AddParameterWithValue(comm, "@arg0", PersistentObject.ID);
                ObjectPersistenceHelper.AddParameterWithValue(comm, "@rarg1", PersistentObject.TabColl.Deleted[i].TabId);
                comm.ExecuteNonQuery();
            }
        }
        #endregion

        #region Manage Child Objects / Collections
        /// Reads the KYO.DAL.Module from the database corresponding to the object keys passed as parameters
        // <param name="localModuleId">Corresponds to ModuleIntall attribute ModuleId</param
        public KYO.DAL.Module GetModuleObject(int localModuleId) {
            KYO.DAL.Module result = new KYO.DAL.Module();
            ModuleCriteria ModuleCriteriaSingle = new ModuleCriteria();
            ModuleCriteriaSingle.Set_ModuleId(ModuleCriteria.AVAILABLE_OPERATORS.EQUALS, localModuleId);
            ModuleAssembler _tempAssem = new ModuleAssembler();
            result = _tempAssem.ReadSingle(ModuleCriteriaSingle);
            return result;
        }

        /// Reads the KYO.DAL.Tab from the database corresponding to the object keys passed as parameters
        // <param name="localID">Corresponds to ModuleIntall attribute ID</param
        // <param name="local">Corresponds to ModuleIntall attribute </param
        public BusinessLayerHelper.PersistentList<KYO.DAL.Tab> GetTabColl(int localID) {
            BusinessLayerHelper.PersistentList<KYO.DAL.Tab> result = new BusinessLayerHelper.PersistentList<KYO.DAL.Tab>();
            System.Data.Common.DbConnection tempConnection = this.InstanceConnection;
            try {
                tempConnection.Open();
                System.Data.Common.DbCommand comm = tempConnection.CreateCommand();
                comm.CommandText = (TabAssembler.SELECT + " FROM [dbo].[ModuleIntallForTab] INNER JOIN [dbo].[Tab] ON [dbo].[ModuleIntallFor" +
                    "Tab].[TabId] = [dbo].[Tab].[TabId] WHERE [dbo].[ModuleIntallForTab].[ModuleIntal" +
                    "lId] = @arg1");
                ObjectPersistenceHelper.AddParameterWithValue(comm, "@arg1", localID);
                TabAssembler _tempAssem = new TabAssembler();
                result.FillData(_tempAssem.ReadCollectionCustom(comm));
            }
            finally {
                tempConnection.Close();
            }
            return result;
        }
        #endregion

        #region Helper Utilities
        protected override ModuleIntallRowMap fillRowMap(System.Data.Common.DbDataReader reader) {
            ModuleIntallRowMap result = new ModuleIntallRowMap();
            result.ID = reader.GetInt32(reader.GetOrdinal("dboModuleIntallDOTID"));
            result.ModuleId = reader.GetInt32(reader.GetOrdinal("dboModuleIntallDOTModuleId"));
            result.Name = reader.GetString(reader.GetOrdinal("dboModuleIntallDOTName"));
            return result;
        }
        internal override void ClearList() {
            GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Clear();
        }

        public override string Abstract_SELECT {
            get {
                return ("SELECT " + ModuleIntallAssembler.RawFields);
            }
        }
        protected override string InstanceTable {
            get {
                return "[dbo].[ModuleIntall]";
            }
        }
        public const string RawFields = "[dbo].[ModuleIntall].[ID] AS dboModuleIntallDOTID, [dbo].[ModuleIntall].[ModuleId" +
            "] AS dboModuleIntallDOTModuleId, [dbo].[ModuleIntall].[Name] AS dboModuleIntallD" +
            "OTName";
        public const string SELECT = "SELECT [dbo].[ModuleIntall].[ID] AS dboModuleIntallDOTID, [dbo].[ModuleIntall].[M" +
            "oduleId] AS dboModuleIntallDOTModuleId, [dbo].[ModuleIntall].[Name] AS dboModule" +
            "IntallDOTName";
        #endregion
    }
}
